package com.swan.mybatis.mapper.mapper;

import com.swan.mybatis.condition.Condition;
import com.swan.mybatis.condition.SelectOption;
import com.swan.mybatis.core.AutoMapperStatementRegister;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author zongf
 * @date 2024-03-20
 **/
@Slf4j
public class BaseMapper implements ApplicationListener<ContextRefreshedEvent> {

    private Map<Class, IBaseMapper> mappers = new ConcurrentHashMap();

    private IBaseMapper getMapper(Class clz) {
        return mappers.get(clz);
    }

    public int count(Class entityClass, Condition condition) {
        return getMapper(entityClass).count(condition);
    }

    public boolean deleteById(Class entityClass, Object entity) {
        return getMapper(entityClass).deleteById(entity);
    }

    public int deleteByIds(Class entityClass, Collection idList) {
        return getMapper(entityClass).deleteByIds(idList);
    }

    public int deleteList(Class entityClass, Condition condition) {
        return getMapper(entityClass).deleteList(condition);
    }

    public boolean delete(Class entityClass, Object entity) {
        return getMapper(entityClass).delete(entity);
    }

    public int insertList(Class entityClass, Collection entities) {
        return getMapper(entityClass).insertList(entities);
    }

    public boolean insert(Class entityClass, Object entity) {
        return getMapper(entityClass).insert(entity);
    }

    public boolean insertNotNull(Class entityClass, Object entity) {
        return getMapper(entityClass).insertNotNull(entity);
    }

    public Object selectById(Class entityClass, Object entity, SelectOption... options) {
        return getMapper(entityClass).selectById(entity, options);
    }

    public List selectListByIds(Class entityClass, List<Object> list, SelectOption... options) {
        return getMapper(entityClass).selectListByIds(list, options);
    }

    public List selectList(Class entityClass, Condition condition, SelectOption... options) {
        return getMapper(entityClass).selectList(condition, options);
    }

    public Object select(Class entityClass, Condition condition, SelectOption... options) {
        return getMapper(entityClass).select(condition, options);
    }

    public boolean updateById(Class entityClass, Object entity) {
        return getMapper(entityClass).updateById(entity);
    }

    public boolean update(Class entityClass, Object entity) {
        return getMapper(entityClass).update(entity);
    }

    public boolean updateNotNullById(Class entityClass, Object entity) {
        return getMapper(entityClass).updateNotNullById(entity);
    }

    public boolean updateNotNull(Class entityClass, Object entity) {
        return getMapper(entityClass).updateNotNull(entity);
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        AutoMapperStatementRegister.getMapperMapEntity().forEach((mapperClass, entityClass)->{
            mappers.put(entityClass, (IBaseMapper) event.getApplicationContext().getBean(mapperClass));
        });

        log.info("IBaseMapper 数量:{}", mappers.size());
    }
}
